//
//  AppDelegate.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "AppDelegate.h"
#import "JASidePanelController.h"
#import "LeftViewController.h"
#import "HomeViewController.h"
#import "TestViewController.h"
#import "BaseNavController.h"
#import <AFNetworkActivityIndicatorManager.h>
#import "MMGuideController.h"

//####################
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"
//####################

@interface AppDelegate ()
@property (nonatomic,strong) JASidePanelController * viewController;

@end

@implementation AppDelegate

#pragma mark - 新特性逻辑优化
- (BOOL)isNewVersion {
    
    // 2.1获取应用当前的版本号 -> 获取info.plist所有数据, -> 取出版本号
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    
    NSString *currentVersion = info[@"CFBundleShortVersionString"];
    
    // 2.2获取之前存储的版本号
    // - 获取用户偏好设置对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // - 获取之前的版本号
    NSString *oldVersion = [defaults objectForKey:@"app_version"];
    
    // 2.3 比较
    // - 两个字符串相同,进入主程序
    if (oldVersion != nil && [currentVersion isEqualToString:oldVersion]) {
        
        return YES;
        
    } else {
        
        // 存储
        [defaults setObject:currentVersion forKey:@"app_version"];
        
        // 同步
        [defaults synchronize];
        
        // 返回
        return YES;
    }
    
    
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 2.设置根控制器
    if ([self isNewVersion]) {
        // 新特性
        self.window.rootViewController = [[MMGuideController alloc] init];
        
        
    } else {
        // 主程序

        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
        
        //实例化侧滑控制器
        self.viewController = [[JASidePanelController alloc] init];
        
        //设置leftPanel
        self.viewController.leftPanel = [[LeftViewController alloc] init];
        
        //设置centerPanel
        HomeViewController * homeVc = [[HomeViewController alloc] init];
        //    TestViewController * homeVc = [[TestViewController alloc ] init];
        
        self.viewController.centerPanel = [[BaseNavController alloc] initWithRootViewController:homeVc];
        
        //设置侧滑控制器为window的根控制器
        self.window.rootViewController = self.viewController;
        

        
        
        //###################################
        [ShareSDK registerApp:@"iosv1101"
         
              activePlatforms:@[
                                @(SSDKPlatformTypeSinaWeibo),
                                @(SSDKPlatformTypeMail),
                                @(SSDKPlatformTypeSMS),
                                @(SSDKPlatformTypeCopy)]
                     onImport:^(SSDKPlatformType platformType)
         {
             switch (platformType)
             {
                 case SSDKPlatformTypeWechat:
                     [ShareSDKConnector connectWeChat:[WXApi class]];
                     break;
                     
                 default:
                     break;
             }
         }
              onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
         {
             
             switch (platformType)
             {
                 case SSDKPlatformTypeSinaWeibo:
                     //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                     [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                               appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                             redirectUri:@"http://www.sharesdk.cn"
                                                authType:SSDKAuthTypeBoth];
                     break;
                 default:
                     break;
             }
         }];
    }

            [self.window makeKeyAndVisible];
    
    
    
  

    // MARK: - 2.状态栏的设置
    // 1.显示白色效果
    application.statusBarStyle = UIStatusBarStyleLightContent;
    // 2.不要隐藏
    application.statusBarHidden = NO;

    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[NSUserDefaults standardUserDefaults] setBool:0 forKey:@"isLogin"];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}




@end
